package com.soft.security.action;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.soft.core.action.BaseAction;
import com.soft.core.dao.GenericDao;
import com.soft.core.utils.SystemConstant;
import com.soft.security.dao.UserDao;
import com.soft.security.domain.Dept;
import com.soft.security.domain.Job;
import com.soft.security.domain.Role;
import com.soft.security.domain.User;

@Controller("userAction")
@Scope("prototype")
public class UserAction extends BaseAction<User> {

	private UserDao userDao;
	
	private Long roleId;
	private Long deptId;
	private Long jobId;
	
	
	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}
	
	@Resource
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public GenericDao<User> getDao() {
		return userDao;
	}
	
	@Override
	public void beforFind(DetachedCriteria criteria) {
		if (null != model.getUserName() && !"".equals(model.getUserName())) {
			criteria.add(Restrictions.like("userName", model.getUserName(),
					MatchMode.ANYWHERE));
		}
	}
	
	@Override
	public void afterToUpdate(User user) {
		List<Role> roles = userDao.find(DetachedCriteria.forClass(Role.class));
		Set<Role> userRoles = user.getRoles();
		for (Role role : userRoles) {
			roles.remove(role);
		}
		ActionContext.getContext().put("roleList", roles);
		
		List<Dept> depts = userDao.find(DetachedCriteria.forClass(Dept.class));
		Set<Dept> userDepts = user.getDepts();
		for (Dept dept : userDepts) {
			depts.remove(dept);
		}
		ActionContext.getContext().put("deptList", depts);
		
		List<Job> jobs = userDao.find(DetachedCriteria.forClass(Job.class));
		Set<Job> userJobs = user.getJobs();
		for (Job job : userJobs) {
			jobs.remove(job);
		}
		ActionContext.getContext().put("jobList", jobs);
	}
	
	public String addRole() {
		User user = userDao.get(model.getId());
		Role role = userDao.get(Role.class, roleId);
		
		role.getUsers().add(user);
		userDao.save(role);
		
		return UPDATE_ACTION;
	}
	
	public String deleteRole() {
		User user = userDao.get(model.getId());
		Role role = userDao.get(Role.class, roleId);
		
		role.getUsers().remove(user);
		userDao.save(role);
		
		return UPDATE_ACTION;
	}	
	
	public String addDept() {
		User user = userDao.get(model.getId());
		Dept dept = userDao.get(Dept.class, deptId);
		user.getDepts().add(dept);
		userDao.save(user);
		return UPDATE_ACTION;
	}
	
	public String deleteDept() {
		User user = userDao.get(model.getId());
		Dept dept = userDao.get(Dept.class, deptId);
		user.getDepts().remove(dept);
		userDao.save(user);
		return UPDATE_ACTION;
	}
	
	public String addJob() {
		User user = userDao.get(model.getId());
		Job job = userDao.get(Job.class, jobId);
		user.getJobs().add(job);
		userDao.save(user);
		return UPDATE_ACTION;
	}
	
	public String deleteJob() {
		User user = userDao.get(model.getId());
		Job job = userDao.get(Job.class, jobId);
		user.getJobs().remove(job);
		userDao.save(user);
		return UPDATE_ACTION;
	}		
	
	/**
	 * 登陆
	 * @return
	 */
	public String login() {
		User user = userDao.login(model.getUserName(), model.getPassword());
		if (null != user) {
			ActionContext.getContext().getSession().put(SystemConstant.CURRENT_USER, user);
			return INDEX;
		} else {
			return LOGIN;
		}
	}
	
	/** 
	 * 注销
	 * @return
	 */
	public String logout() {
		ActionContext.getContext().getSession().remove(SystemConstant.CURRENT_USER);
		return LOGIN;
	}
	
	public void setDeptId(Long deptId) {
		this.deptId = deptId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}	
	
}
